package com.swdc.dc;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class Main extends Activity {
	
	private static int PICK_CONTACT_REQUEST = 0;
	private ArrayList<String> active_chats;
	private ArrayAdapter<String> list_adapter;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        active_chats = new ArrayList<String>();
        init();
        
        ListView chat_list = (ListView) findViewById(R.id.menu_chat_list);
        Button chat_add = (Button) findViewById(R.id.menu_add_chat);
        
        chat_list.setTextFilterEnabled(true);
        
        list_adapter = new ArrayAdapter<String>(this, R.layout.list_item, active_chats);
        
        chat_list.setAdapter(list_adapter);
        
        chat_list.setOnItemClickListener(new OnItemClickListener() {
        	
        	public void onItemClick(AdapterView<?> parent, View view, int position, long id){
        		
        		Toast.makeText(Main.this, "Item " + position + " was clicked!", Toast.LENGTH_SHORT).show();
        	}
		});
        
        chat_list.setOnItemLongClickListener(new OnItemLongClickListener() {

			public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				
        		Toast.makeText(Main.this, "Item " + arg2 + " was long clicked!", Toast.LENGTH_SHORT).show();
				
				return false;
			}
        	
        	
		});
        
        chat_add.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				
				startActivityForResult(new Intent(Main.this, ContactPicker.class), PICK_CONTACT_REQUEST);
			}
		});
    }

    public boolean onKeyDown(int keycode, KeyEvent event){
    	
		if(keycode == KeyEvent.KEYCODE_BACK){
			
			Main.this.finish();
			
//			DEBUG OUTPUT
			System.out.println("Finishing now");
		}
		else
			return super.onKeyDown(keycode, event);
		
		return true;
	}
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
    	
    	if (requestCode == PICK_CONTACT_REQUEST) {
          
    		if (resultCode == RESULT_OK) {
                // A contact was picked.
            	String input = data.getExtras().getString(ContactPicker.NAME_CODE);
    			
    			active_chats.add(input);
    			
//    			DEBUG OUTPUT
    			System.out.println("Retrieving name: " + input);
    			list_adapter.notifyDataSetChanged();
            }
    	}
    }
    
    private void init(){
    	
    }
}
